package com.ztgf.file.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.file.models.bo.FileWatermarkSaveBO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import com.ztgf.file.models.service.IFileWatermarkService;
import com.ztgf.file.models.bo.FileWatermarkBO;
import com.ztgf.file.models.dto.FileWatermarkDTO;

import java.util.Arrays;
import java.util.List;

/**
 * 水印管理控制器
 *
 * @author mxli
 * @Date 2020-07-24 14:23:21
 */

@RestController
@RequestMapping("/api/fileWatermark")
@Slf4j
@Api(value = "FileWatermarkApiController",tags = "水印管理Api接口")
public class FileWatermarkApiController {

    @DubboReference
    private IFileWatermarkService fileWatermarkService;

    @PostMapping("/save")
    @ApiOperation(value = "保存水印管理",notes = "保存水印管理")
    public Result save(FileWatermarkSaveBO saveBO){
        //更加platId 与shopId 与 type
        if(saveBO.getType() == 0){ //文字
            if(StrUtils.isEmpty(saveBO.getWaterName()) || StrUtils.isEmpty(saveBO.getFontName())
                    || saveBO.getFontStyle() == null
                    || saveBO.getFontSize() <= 0 || StrUtils.isEmpty(saveBO.getFontColor())
                    ){
                return new Result<>(CodeEnum.FAIL_100002);
            }
        } else {
            if(StrUtils.isEmpty(saveBO.getImgUrl())){
                return new Result<>(CodeEnum.FAIL_100002);
            }
        }
        if(saveBO.getIsUse() == null){
            return new Result<>(CodeEnum.FAIL_100002);
        }

        if(!(saveBO.getTransparency() >=0 && saveBO.getTransparency() <= 100)){
            return new Result<>(CodeEnum.FAIL_100002);
        }

        if(!(saveBO.getInclination() >= -90 && saveBO.getInclination() <= 90)){
            return new Result<>(CodeEnum.FAIL_100002);
        }

        if(saveBO.getDatumPoint() < 0){
            return new Result<>(CodeEnum.FAIL_100002);
        }


        boolean flag = fileWatermarkService.save(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100011);
        }
    }

    @ApiOperation(value = "删除水印管理",notes = "删除水印管理")
    @ApiImplicitParam(name = "id",value = "水印管理id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = fileWatermarkService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除水印管理",notes = "批量删除水印管理")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        fileWatermarkService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<FileWatermarkDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        FileWatermarkDTO dto = fileWatermarkService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "水印管理列表",notes = "水印管理列表")
    @PostMapping("/queryPageList")
    public Result<Page<FileWatermarkDTO>> queryPageList(FileWatermarkBO bo){
        Wrapper<FileWatermarkDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("file_user_id", bo.getFileUserId());
        wrapper.eq("child_user_id", bo.getChildUserId());
        Page<FileWatermarkDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = fileWatermarkService.selectPage(page, wrapper);
        return new Result<>(page);
    }

    @ApiOperation(value = "获取用户自定义水印",notes = "获取用户自定义水印")
    @PostMapping("/getInfo")
    public Result<FileWatermarkDTO> getInfo(FileWatermarkBO bo){
        Wrapper<FileWatermarkDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("file_user_id", bo.getFileUserId());
        wrapper.eq("child_user_id", bo.getChildUserId());
        wrapper.eq("type", bo.getType());
        FileWatermarkDTO dto = fileWatermarkService.selectOne(wrapper);
        return new Result<>(dto);
    }
}
